package com.ccx.web.front;


import com.ccx.model.EbUser;
import com.ccx.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class LoginController {
    @Autowired

    private UserService userService;

    //登录
    @RequestMapping("/to_login")
    public String toLogin() {
        return "login";//login.jsp

    }

    @RequestMapping("/login")
    public String login(HttpServletRequest request, String userName, String passWord) {
        System.out.println("userName=" + userName+"\tpassWord="+passWord );
        EbUser lgnUser = userService.login(userName,passWord);

        System.out.println("lgnUser=" + lgnUser);

        HttpSession session = request.getSession();
        if (lgnUser == null) {
            //保存一个登录失败的标记
            session.setAttribute("lgn_error", "lgn_error");
            //重新登录
            return "redirect:to_login";
        }

        //2.将用户放入session中
        session.setAttribute("lgnUser", lgnUser);
        //清除错误提示
        session.removeAttribute("lgn_error");
        //3.重新成功往首页跳转
        return "redirect:front/index";

    }


    }
